#!/usr/bin/perl

use Foundation;

sub findV3ResPath;
sub findV3Resources;

# Look for any V3 resources and pass the path back to the postinstall script
$resPathToUse = findV3Resources;
print $resPathToUse;

# Sub routine to find settings.plist and key value to search for
sub findV3Resources
{
	# Look for V3 resources first from the settings.plist...
	if ( -e $ENV{HOME} . "/Documents/Promethean/Activstudio3/settings.plist" )
	{
		$file = $ENV{HOME} . "/Documents/Promethean/Activstudio3/settings.plist";
	}
	else
	{
		if ( -e $ENV{HOME} . "/Documents/Promethean/Activprimary3/settings.plist" )
		{
			$file = $ENV{HOME} . "/Documents/Promethean/Activprimary3/settings.plist";
		}
	}					
			
	$plist = NSDictionary->dictionaryWithContentsOfFile_( $file );

	if ( $plist and $$plist) 
	{
		$plistKey = "PROFILE AND RESOURCES_English";
		$userResPath = findV3ResPath( $plist, $plistKey, "Shared files location" );
 
		if ( $userResPath and $$userResPath ) 
		{
			$resPathFound = $userResPath->description()->UTF8String();
		} 
		else 
		{
			$plistKey = "PROFILE AND RESOURCES_French"; 
			$userResPath = findV3ResPath( $plist, $plistKey, "Shared files location" );
			if ( $userResPath and $$userResPath ) 
			{
				$resPathFound = $userResPath->description()->UTF8String();
			} 
		}		

	} 
	return $resPathFound;
}	

# Sub routine to return V3 resources path value found in settings.plist
sub findV3ResPath 
{
  my ( $object, @keysIndexes ) = ( @_ );
  if ( @keysIndexes ) 
  {
    foreach my $keyIndex ( @keysIndexes ) 
	{
      if ( $object and $$object ) 
	  {
        if ( $object->isKindOfClass_( NSArray->class ) ) 
		{
          $object = $object->objectAtIndex_( $keyIndex );
        } 
		elsif ( $object->isKindOfClass_( NSDictionary->class ) ) 
		{
          $object = $object->objectForKey_( $keyIndex );
        } 
      }
    }
  }
  return $object;
}
